package com.wlstock.funi.widget;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wlstock.funi.R;
import com.wlstock.funi.model.OperateTrackSetModel;
import com.wlstock.funi.utils.CaculateUtils;

public class SpecificSetItemView extends LinearLayout implements OnCheckedChangeListener {
	private OperateTrackSetModel mode;
	private EditText ed_b_offset,ed_b_money,ed_s_offset;
	private CheckBox cb_b_offeset,cb_s_offeset;
	
	public SpecificSetItemView(Context context) {
		super(context);
	}
	
	public SpecificSetItemView(Context context, AttributeSet set,OperateTrackSetModel mode){
		super(context, set);
		View coverView = LayoutInflater.from(context).inflate(R.layout.list_item_specific_set, this);
		TextView tv_info = (TextView) coverView.findViewById(R.id.tv_info);
		ed_b_offset = (EditText) coverView.findViewById(R.id.ed_b_offset);
		cb_b_offeset = (CheckBox) coverView.findViewById(R.id.cb_b_offset);
		ed_b_money = (EditText) coverView.findViewById(R.id.ed_b_money);
		ed_s_offset = (EditText) coverView.findViewById(R.id.ed_s_offset);
		cb_s_offeset = (CheckBox) coverView.findViewById(R.id.cb_s_offset);
		
		this.setMode(mode);
		
		tv_info.setText(mode.getFundName() +"\t本期:"+ CaculateUtils.getPercentByFloat( mode.getProfitRate() ));
		updateSetItem(mode);
		
		ed_b_offset.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable text) {
				// TODO Auto-generated method stub
				float f = getFloatVal(text);
				SpecificSetItemView.this.getMode().buyOffset = f;
			}

	
		});
		
		ed_b_money.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub
				float f = getFloatVal(arg0);
				SpecificSetItemView.this.getMode().buyMoney = f;
			}
		});
		
		ed_s_offset.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub
				float f = getFloatVal(arg0);
				SpecificSetItemView.this.getMode().sailOffset = f;
			}
		});
		
		cb_b_offeset.setOnCheckedChangeListener(this);
		cb_s_offeset.setOnCheckedChangeListener(this);
		this.setTag(mode);
	}

	public void updateSetItem(OperateTrackSetModel mode) {
		ed_b_offset.setText(mode.buyOffset+"");
		cb_b_offeset.setChecked(mode.isTrackBuy);
		ed_b_money.setText(mode.buyMoney+"");
		ed_s_offset.setText(mode.sailOffset+"");
		cb_s_offeset.setChecked(mode.isTrackSaile);
	}
	
	@Override
	public void onCheckedChanged(CompoundButton arg0, boolean value) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.cb_b_offset:
			mode.isTrackBuy = value;
			break;
		case R.id.cb_s_offset:
			mode.isTrackSaile = value;
			break;
		default:
			break;
		}
	}
	
	private float getFloatVal(Editable text) {
		float f = .0f;
		try {
			f = Float.valueOf(text.toString());
		} catch (Exception e) {
		}
		return f;
	}

	public OperateTrackSetModel getMode() {
		return mode;
	}

	public void setMode(OperateTrackSetModel mode) {
		this.mode = mode;
	}
	
	public interface onSaveModeLister{
		public abstract void saveMode(OperateTrackSetModel mode);
	}
}
